﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VFCons
{
	public partial class FTControl : UserControl
	{
		public EventHandler MoveCmdClick;
		public EventHandler BoxCmdClick;

		private List<Button> btns = new List<Button>();
		public FTControl()
		{
			InitializeComponent();

			btns.Add(buttonLeftUp);//4
			btns.Add(buttonUp);//0
			btns.Add(buttonRightUp);//5
			btns.Add(buttonLeft);//2
			btns.Add(buttonJL);//7
			btns.Add(buttonRight);//3
			btns.Add(buttonLeftDown);//6
			btns.Add(buttonDown);//1
			btns.Add(buttonRightDown);//8
			btns.Add(buttonHor);//10
			btns.Add(buttonVer);//9
			btns.Add(buttonDS);//11
			btns.Add(buttonFM);//12
			btns.Add(buttonGroup1);//13
			btns.Add(buttonGroup2);//14
			btns.Add(buttonStop);//15

			for (int i = 0; i < cmds.Length && i < btns.Count; i++)
			{
				btns[i].Tag = cmds[i];
			}

			foreach (var tbn in btns)
				tbn.Click += Buttons_Click;

			//
			labelFreq.Text = "料仓频率" + Environment.NewLine + "(10.0~400.0)Hz";
			labelVoltage.Text = "料仓电压" + Environment.NewLine + "(0.0~24.0)V";
			buttonBoxON.Tag = 0;
			buttonBoxOFF.Tag = 1;
			buttonSet.Tag = 2;

			buttonBoxON.Click += BoxButtons_Click;
			buttonBoxOFF.Click += BoxButtons_Click;
			buttonSet.Click += buttonSet_Click;
			Freq = 0;
			Voltage = 0;
		}

		#region move
		//private int[] cmds = new int[16] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
		private int[] cmds = new int[16] { 4, 0, 5, 2, 7, 3, 6, 1, 8, 10, 9, 11, 12, 13, 14, 15 };
		public int[] CMDs
		{
			get { return cmds; }
			set
			{
				cmds = value;
				for(int i = 0; i < cmds.Length&&i < btns.Count;i++)
				{
					btns[i].Tag = cmds[i];
				}
			}
		}
		private void Buttons_Click(object sender, EventArgs e)
		{
			if (MoveCmdClick != null) MoveCmdClick(sender, e);
		}

		private ushort _freq = 0;
		public ushort Freq
		{
			get { return _freq; }
			set
			{
				_freq = value;
				textBoxFreq.Text = (_freq * 0.1F).ToString("F1");
			}
		}

		private ushort _voltage = 0;
		public ushort Voltage
		{
			get { return _voltage; }
			set
			{
				_voltage = value;
				textBoxVoltage.Text =(_voltage * 0.1F).ToString("F1");
			}
		}
		#endregion move


		private void buttonSet_Click(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(textBoxFreq.Text))
			{
				MessageBox.Show("频率的有效值是(10.0~400.0)Hz", "Error");
				textBoxFreq.Focus();
				return;
			}

			if (string.IsNullOrEmpty(textBoxVoltage.Text))
			{
				MessageBox.Show("电压的有效值是(0.0~2.4.0)V", "Error");
				textBoxVoltage.Focus();
				return;
			}

			ushort f = _freq;
			ushort v = _voltage;
			try
			{
				f = Convert.ToUInt16(Convert.ToSingle(textBoxFreq.Text) * 10);
			}
			catch (Exception ex)
			{
				MessageBox.Show("频率的有效值是(10.0~400.0)Hz" + Environment.NewLine + ex.Message, "Error");
				textBoxFreq.Focus();
				textBoxFreq.SelectAll();
			}

			try
			{
				v = Convert.ToUInt16(Convert.ToSingle(textBoxVoltage.Text) * 10);
			}
			catch (Exception ex)
			{
				MessageBox.Show("电压的有效值是(0.0~2.4.0)V" + Environment.NewLine + ex.Message, "Error");
				textBoxVoltage.Focus();
				textBoxVoltage.SelectAll();
			}

			_freq = f;
			_voltage = v;
			BoxButtons_Click(sender, e);

		}


		private void BoxButtons_Click(object sender, EventArgs e)
		{
			if (BoxCmdClick != null) BoxCmdClick(sender, e);
		}
	}
}
